In computer science and computer programming, a data type or simply type is a classification of data which tells the compiler or interpreter how the programmer intends to use the data. Most programming languages support various types of data, for example:integer, float or Boolean. A data type provides a set of values from which an expression (i.e. variable, function...) may take its values. This data type defines the operations that can be done on the data, the meaning of the data, and the way values of that type can be stored. A type of value from which an expression may take its value.
在计算机科学和计算机编程中,一种数据类型是对数据的一种分类,它能告诉编译器或解释器,编程人员意图怎样使用数据。放多编程语言支持多种数据类型,如整型、实型或布尔型。一种数据类型可以为表达式(变量或函数...)提供一组值。这个数据类型可以定义数据上的特定操作、数据的含义、数据类型值存储的方法。一种数据类型的值可以给到一个表达式。
In the C programming language, data types are declarations for memory locations or variables that determine the characteristics of the data that may be stored and the methods (operations) of processing that are permitted involving them.
The C language provides basic arithmetic types, such as integer and real number types, and syntax to build array and compound types. Several headers in the C standard library contain definitions of support types, that have additional properties, such as providing storage with an exact size, independent of the implementation.
Different data types requires different amounts of memory and therefore vary in the range of values they can store.
不同的数据类型战胜不同大小的内存空间,因此,它们所能表示的数据的范围也各不相同。
Any type that does not specify an implementation is an abstract data type. For instance, a stack (which is an abstract type) can be implemented as an array (a contiguous block of memory containing multiple values), or as a linked list (a set of non-contiguous memory blocks linked by pointers).
没有定义具体实现的属于抽象数据类型。例如,栈可以用数组或链表实现。
Abstract types can be handled by code that does not know or "care" what underlying types are contained in them. Programming that is agnostic about concrete data types is called generic programming. Arrays and records can also contain underlying types, but are considered concrete because they specify how their contents or elements are laid out in memory.
抽象数据类型不关心其基本类型构成。元编程就是一种不关心具体类型的编程方式。数组和记录都包含有基本的数据类型,但是也有具体描述数组元素或内容在内存中的存储。
Examples include:
A queue is a first-in first-out list. Variations are Deque and Priority queue.
A set can store certain values, without any particular order, and with no repeated values.
A stack is a last-in, first out data structure.
A tree is a hierarchical structure.
A graph.
A hash, dictionary, map or associative array is a more flexible variation on a record, in which name-value pairs can be added and deleted freely.
A smart pointer is the abstract counterpart to a pointer. Both are kinds of references.
The items of information that make up an array have all the same data type(int, float etc.) and are logically related in some way. For example, a student's test scores may be integer values that are logically related to the student. In this case it makes sense to store the test scores together in an array. In short, arrays are suitable for storing sets of homogeneous data.
构成一个数组的各信息项在某种程度上都是逻辑相关的,并且具有相同的数据类型(整形、实型等)。例如,一个学生的考试成绩与其他学生是逻辑相关的,并且具有整型值。在这种情况下,将考试成绩存储在一个数组中是比较合适的。简而言之,数组适合于存储同种类型的数据集。
For every type T, except void and function types, there exist the types "array of N elements of type T". An array is a collection of values, all of the same type, stored contiguously in memory. An array of size N is indexed by integers from 0 up to and including N-1. There are also "arrays of unspecified size" where the number of elements is not known by the compiler. Here is a brief example:
除了空类型或函数类型,每一种类型T,都存在一种“有N个元素的类型T的数组”。一个数组是一种数组类型的值的集合,所有相同类型,在内存中连续存储。一个N个元素的数组从0开始索引到N-1。当然,数组的大小也可以不预先定义给编译器。下面是一个简单的数组例子:
int cat[10]; // array of 10 elements, each of type int
int bob[]; // array of an unspecified number of 'int' elements
Arrays are passed to functions by passing a pointer to the first element. Multidimensional arrays are defined as "array of array …", and all except the outermost dimension must have compile-time constant size:
数组可以可以通过一个指向第一个元素的指针传递给一个函数。多重数组是指“数组的数组...”,多重数组最外层的维需要在编译时间有一个常量的值表明其大小。
int a[10][8]; // array of 10 elements, each of type 'array of 8 int elements'
float f[][32]; // array of unspecified number of 'array of 32 float elements'
However, there are items of information that are logically related but each item may have a different data type. A student's number and test scores, for example, are logically related to the student, but the number may be an integer, while the test scores may be floating-point values.
然而,还有一些逻辑相关但数据类型不同的信息项,例如,学生的学号和考试成绩是逻辑相关的,但是学号可能是整型,而考试成绩可能是浮点数据类型。
Logically related items of information that may have different data types can be combined into a structure. Unlike an array, the data items in a structure may be of different types.
结构体可以表示逻辑相关但数据类型不同的数据项。与数组不同的是,结构体中的数据项的数据类型可以是不同的。
Structures aggregate the storage of multiple data items, of potentially differing data types, into one memory block referenced by a single variable. The following example declares the data type struct birthday which contains the name and birthday of a person. The structure definition is followed by a declaration of the variable John that allocates the needed storage.
结构体通过引用一个变量、存储多种数据元素,或者不同的数据类型到一个内存块。下面的例子声明了一个结构体类型“birthday”,这个结构体包含一个人的姓名和生日。紧随其后的的结构体定义是变量“John”,并分配需要大小的内存。
struct birthday { char name[20]; int day; int month; int year; }; struct birthday John;
A structure containing a pointer to a structure of its own type is commonly used to build linked data structures:
一个结构体可以包含一个指向自身的指针,通常用来建立链接数据结构。
struct node { int val; struct node *next; };